package springsecurity.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author cc
 * @date 2022/6/13 20:04
 * @Description : 在实现类中为用户详情服务，编写用户认证逻辑。
 */
@Service
public class UserDetailServiceimpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        if (!s.equals("admin")){
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        //把参数按照特定的解析规则进行解析
        String password = passwordEncoder.encode("123");

        //  commaSeparatedStringToAuthorityList方法: 将字符串分割，转化为权限列表，默认是用 逗号 作为分隔符
        return new User("admin",password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,test"));
    }
}
